package com.jpm.jrogue.graphics
{
	import com.jpm.jrogue.factories.ImageFactory;
	import com.jpm.jrogue.Game;
	import com.jpm.jrogue.Player;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	/**
	 * ...
	 * @author Jonboy
	 */
	public class BaseImage extends Image
	{
		public var tile:Tile;
		
		[Embed(source="../assets/L1_Terrain001.PNG")]
		private const TerrainMask:Class;
		private const terrainBitmapData:BitmapData = (new TerrainMask() as Bitmap).bitmapData;
		private const originPoint:Point = new Point(0, 0);
		
		public function BaseImage(tile:Tile)
		{
			super(ImageFactory.getTexture(tile.base));
			this.tile = tile;
			height = 108;
			width = 108;
			addEventListener(TouchEvent.TOUCH, onTouch);
		}
		
		public override function hitTest(localPoint:Point, forTouch:Boolean = false):DisplayObject
		{
			if (forTouch && (!visible || !touchable))
			{
				return null;
			}
			
			if (getBounds(this).containsPoint(localPoint))
			{
				if (terrainBitmapData.hitTest(originPoint, 0xFF, localPoint))
				{
					return this;
				}
			}
			
			return null;
		}
		
		private function onTouch(event:TouchEvent):void
		{
			if (event.getTouch(this, TouchPhase.HOVER))
			{
				Game.instance.hover(this);
			}
			else if (event.getTouch(this, TouchPhase.ENDED))
			{
				Game.instance.moveTo(tile);
			}
		
		}
	}
}